package com.yf.exam.common.entity;


import com.yf.exam.common.enums.ResultCodeEnum;
import com.yf.exam.core.exception.ServiceException;
import lombok.Data;

/**
 * @description:
 * @author: gengjiaxuan
 * @createDate: 2024/11/12
 * @version: 1.0
 */
@Data
public class Result<T> {

    private Boolean success;

    private String message;

    private Integer code;

    private T data;

    public static Result ok() {
        Result result = new Result();
        result.setSuccess(true);
        result.setCode(ResultCodeEnum.SUCCESS.getCode());
        result.setMessage(ResultCodeEnum.SUCCESS.getDesc());
        return result;
    }

    public static <T> Result ok(T data) {
        Result result = new Result();
        result.setSuccess(true);
        result.setCode(ResultCodeEnum.SUCCESS.getCode());
        result.setMessage(ResultCodeEnum.SUCCESS.getDesc());
        result.setData(data);
        return result;
    }

    public static Result faild() {
        Result result = new Result();
        result.setSuccess(false);
        result.setCode(ResultCodeEnum.FAILD.getCode());
        result.setMessage(ResultCodeEnum.FAILD.getDesc());
        return result;
    }

    public static <T> Result faild(T data) {
        Result result = new Result();
        result.setSuccess(false);
        result.setCode(ResultCodeEnum.FAILD.getCode());
        result.setMessage(ResultCodeEnum.FAILD.getDesc());
        result.setData(data);
        return result;
    }
    /**
     * 构造函数
     * @param error
     */
    public static Result faild(ServiceException error){
        Result result = new Result();
        result.setCode(error.getCode());
        result.setMessage(error.getMsg());
        return result;
    }


}
